package com.dyb.idempotent.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;

/**
 * @ProjectName: idempotent
 * @Package: com.dyb.idempotent.service
 * @ClassName: RedisTokenService
 * @Author: Deng YuBin
 * @Description:
 * @Date: 2021/1/14 16:42
 * @Version: 1.0
 */
@Service
public class RedisTokenService {

    private static final Integer TOKEN_TIMEOUT = 600;

    @Autowired
    private RedisService redisService;

    public String getToken() {
        String token = "token" + UUID.randomUUID();
        redisService.set(token, token, TOKEN_TIMEOUT);
        return token;
    }

    public boolean findToken(String tokenKey){
        String token = redisService.get(tokenKey);
        if(StringUtils.isEmpty(token)){
            return false;
        }
        redisService.del(tokenKey);
        return true;
    }

    public String see(String key){
        return redisService.see(key);
    }
}
